package com.bjtct.plugin.autogen.wizards;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.bjtct.plugin.autogen.db.ColumnWrap;
import com.bjtct.plugin.autogen.db.DbFactory;
import com.bjtct.plugin.autogen.db.IDataBase;
import com.bjtct.plugin.autogen.wizards.consts.Consts;
import com.bjtct.plugin.autogen.wizards.priovider.CellModifyer;
import com.bjtct.plugin.autogen.wizards.priovider.TableViewContent;
import com.bjtct.plugin.autogen.wizards.priovider.TableViewerLabel;

public class TableDetailSettingPage extends WizardPage {
	private ISelection selection;
	private Table table;

	public TableDetailSettingPage(ISelection selection) {
		super("tspage");
		this.selection = selection;
		setTitle("表详细设置");
		setDescription("为表中的每个字段进行详细设置");
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		String name = loadData("selectTable");

		TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(0, 0, 692, 312);

		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tableColumn = tableViewerColumn.getColumn();
		tableColumn.setAlignment(SWT.LEFT);
		tableColumn.setWidth(80);
		tableColumn.setText("列名称");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tableColumn_1 = tableViewerColumn_1.getColumn();
		tableColumn_1.setAlignment(SWT.LEFT);
		tableColumn_1.setWidth(71);
		tableColumn_1.setText("别  名");
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn = tableViewerColumn_2.getColumn();
		tblclmnNewColumn.setWidth(81);
		tblclmnNewColumn.setText("显示名称");
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_5.setWidth(60);
		tblclmnNewColumn_5.setText("默认值");

		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_4.setWidth(60);
		tblclmnNewColumn_4.setText("数据类型");

		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_3.setWidth(60);
		tblclmnNewColumn_3.setText("表单类型");
		tblclmnNewColumn_3.setToolTipText("文件框、下拉框、日期框、日期时间框");

		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_2.setWidth(60);
		tblclmnNewColumn_2.setText("条件查询");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.CENTER);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_1.setWidth(60);
		tblclmnNewColumn_1.setText("列表显示");

		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_8.getColumn();
		tblclmnNewColumn_6.setWidth(60);
		tblclmnNewColumn_6.setText("修改显示");
		TableViewerColumn tableViewerColumn_9 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_2 = tableViewerColumn_9.getColumn();
		tableColumn_2.setWidth(60);
		tableColumn_2.setText("新增显示");
		
		tableViewer.setContentProvider(new TableViewContent());
		tableViewer.setLabelProvider(new TableViewerLabel());
		tableViewer.setInput(loadColumns());

		tableViewer.setColumnProperties(new String[] { "column_name", "column_alis", "column_comment", "column_default",
				"data_type", "is_nullable", "list_query", "list_show", "update_show","new_show" });

		CellEditor[] ced = new CellEditor[10];
		ced[0] = null;
		ced[1] = new TextCellEditor(tableViewer.getTable());
		ced[2] = new TextCellEditor(tableViewer.getTable());
		ced[3] = new TextCellEditor(tableViewer.getTable());
		ced[4] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.JAVA_TYPE);
		ced[5] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.PAGE_TYPE);
		ced[6] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.YES_NO);
		ced[7] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.YES_NO);
		ced[8] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.YES_NO);
		ced[9] = new ComboBoxCellEditor(tableViewer.getTable(), Consts.YES_NO);

		tableViewer.setCellEditors(ced);
		tableViewer.setCellModifier(new CellModifyer(tableViewer));
		

		
		

		setControl(container);
		if (checkAutoGened(name)) {
			boolean yOn = MessageDialog.openConfirm(getShell(), "已经生成", name + " 已经生成，确认要重新 生成吗？");
			if (!yOn) {
				setPageComplete(false);
			}
		}

	}

	private List<ColumnWrap> loadColumns() {
		List<ColumnWrap> columns = new ArrayList<ColumnWrap>();
		IDialogSettings dset = getSet();
		try {
			String selectTable = dset.get("selectTable");
			dset.load("Tct.xml");
			String user = dset.get("dbuser");
			String dbpwd = dset.get("dbpwd");
			String url = dset.get("url");
			String dbtype = dset.get("dbtype");
			IDataBase db = DbFactory.getDataBase(dbtype);
			columns.addAll(db.getColumns(url, user, dbpwd, selectTable));

		} catch (IOException e) {

			e.printStackTrace();
		}
		return columns;
	}

	private String loadData(String name) {
		IDialogSettings ds = getSet();
		String value = null;
		if (ds != null) {
			value = ds.get(name);
		}
		return value;
	}

	private boolean checkAutoGened(String tablename) {
		boolean result = false;// 未生成
		IDialogSettings ds = getSet();

		try {
			ds.load("Tct.xml");
			String[] tables = ds.getArray("autogened");
			if (tables != null)
				 for(int i=0;i<tables.length;i++){
					 if(tables[i].equals(tablename)){
						 result = true;
						 break;
					 }
				 }
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

		return result;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		setControl(null);
	}

	private IDialogSettings getSet() {
		return getWizard().getDialogSettings();
	}

	public ISelection getSelection() {
		return selection;
	}

	public void setSelection(ISelection selection) {
		this.selection = selection;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}
}
